package com.paratice.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class CuratorWatcherDemo {
	
	/**
	 * PathChildrenCache监听一个节点子节点的新增、修改、删除事件
	 * NodeCache监听一个节点地新增和更新事件
	 * TreeCache综合PathChildrenCache和NodeCache的特性
	 */
	public static void main(String[] args) {
		try {
			CuratorFramework curatorFramework = CuratorFrameworkFactory
					.builder().connectString("192.168.2.200:2181")
					.sessionTimeoutMs(4000)
					.retryPolicy(new ExponentialBackoffRetry(1000, 3))
					.namespace("curator").build();
			curatorFramework.start();
			//当前节点的创建和更新事件监听
//			addListenerWithNodeCache(curatorFramework, "/zk-persis");
			
			//子节点新增、修改、删除事件监听
//			addListenerWithPathChildCache(curatorFramework, "/zk-persis");
			
			//综合节点监听事件
			addListenerWithTreeCache(curatorFramework, "/zk-persis");
			
			System.in.read();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void addListenerWithTreeCache(CuratorFramework curatorFramework,String path) {
        try {
        	TreeCache treeCache=new TreeCache(curatorFramework,path);
        	TreeCacheListener treeCacheListener=new TreeCacheListener() {
        		@Override
        		public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
        			System.out.println(event.getType()+"->"+event.getData().getPath());
        		}
        	};
        	
        	treeCache.getListenable().addListener(treeCacheListener);
        	treeCache.start();
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
	
	public static void addListenerWithPathChildCache(CuratorFramework curatorFramework, String path) {
		try {
			PathChildrenCache cache = new PathChildrenCache(curatorFramework, path, true);
			PathChildrenCacheListener listener = new PathChildrenCacheListener() {
				
				@Override
				public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
					System.out.println("Receive Event : " + event.getType() + " -> " + event.getData().getPath());
				}
			};
			cache.getListenable().addListener(listener);
			cache.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void addListenerWithNodeCache(CuratorFramework curatorFramework, String path) {
		try {
			NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
			NodeCacheListener listener = new NodeCacheListener() {
				@Override
				public void nodeChanged() throws Exception {
					System.out.println("Receive Event:" + nodeCache.getCurrentData().getPath());
				}
			};
			nodeCache.getListenable().addListener(listener);
		
			nodeCache.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
